<?php

/**
 * This is the model class for table "tb_questions".
 *
 * The followings are the available columns in table 'tb_questions':
 * @property integer $ques_id
 * @property string $ques_name
 * @property string $ques_created
 */
class Questions extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tb_questions';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('ques_name, ques_created', 'required'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('ques_id, ques_name, ques_created', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array("Ans" => array(self::BELONGS_TO, "Answers", "ques_id"),
                             "Cho" => array(self::BELONGS_TO, "Choice", "ques_id")
                    );
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ques_id' => 'ลำดับที่',
			'ques_name' => 'คำถาม',
			'ques_created' => 'วันที่',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
                $criteria->order = "ques_created DESC";
		/*$criteria->compare('ques_id',$this->ques_id);
		$criteria->compare('ques_name',$this->ques_name,true);
		$criteria->compare('ques_created',$this->ques_created,true);*/

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function number($data, $row)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.
                $i = 0;
                while($i<=$row){
                    $i++;
                }
                return $i;
	}
        
        public function choice1($data, $row)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.
                $criteria = new CDbCriteria();
                $criteria->select = "count(ans_name) as ans_name from tb_answers where ques_id = '$data' and ans_name = 'ชอบที่สุด'";
                
                $counts = Questions::model()->find($criteria);
                //$i = 3;
                return $counts;
	}
        
        public function getButtonEdit($data, $row) {
        //$answer = new Answers();
        $id = Yii::app()->session["user_id"];
        //$criteria = new CDbCriteria();
        //$criteria->compare($data->ques_id, $id);
        $cntCriteria = new CDbCriteria();
        
        $cntCriteria->condition = "ques_id = :ques_id";
        $cntCriteria->addCondition("id = :id");
         $cntCriteria->params[':ques_id'] = $data->ques_id;
         $cntCriteria->params[':id'] = $id;
         
         $articleCount = Answers::model()->count($cntCriteria);
        if($articleCount == 0){
            $url = array("Answers/Answers", "id" => $data->ques_id);
            //$link = CHtml::link("edit", $url);
            //$link1 = Yii::app()->createUrl($url);
            
            $imageUrl = Yii::app()->request->baseUrl."/images/like.png";
            $image = '<img src="'.$imageUrl.'"/>';
            $link = CHtml::link($image, $url);
            
            return $link;
        }
        return NULL;
        
    } 

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Questions the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
